<template>
  <el-container class="el-con">
    <el-header class="bt">
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="tuichu">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="iscollapse ? '64px' : '200px'">
          <div @click="changeCd" class="changCd">|||</div>
        <el-menu
        :default-active="isGlian"
        unique-opened
        router
        :collapse="iscollapse"
        :collapse-transition="false"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="skyblue">
          <el-submenu v-for="item in cdlist" :key="item.id" :index="item.id + ''">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item @click="isGl('/' + item1.path)" v-for="item1 in item.children" :key="item1.id" :index="'/' + item1.path"  class="zcd">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{item1.authName}}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
          <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      cdlist: [],
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      iscollapse: false,
      isGlian: ''
    }
  },
  created () {
    this.getCDlist()
    this.isGlian = sessionStorage.getItem('isGl')
  },
  methods: {
    tuichu () {
      this.$router.push('/login')
      sessionStorage.removeItem('token', 0)
    },
    async getCDlist () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取菜单列表失败')
      this.cdlist = res.data
    },
    changeCd () {
      this.iscollapse = !this.iscollapse
    },
    isGl (id) {
      sessionStorage.setItem('isGl', id)
      this.isGlian = id
    }
  }
}
</script>

<style scoped lang="less">
.el-con {
  height: 100%;
}
.el-header {
  background-color: skyblue;
  > div {
    font-size: 20px;
    color: #fff;
    img {
      width: 60px;
      height: 60px;
      vertical-align: middle;
    }
  }
}
.bt {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-aside {
  background-color: #545c64;
}
.iconfont {
    margin-right: 5px;
}
.zcd {
    margin-left: 10px;
}
.el-menu {
    border: 0;
    padding-right: 20px;
}
.changCd {
    color: #fff;
    font-size: 20px;
    text-align: center;
}
</style>
